package com.seu.vcampus.view.library;

import com.seu.vcampus.client.VCampusClient;
import com.seu.vcampus.util.UIStyleUtil;
import com.seu.vcampus.vo.Book;
import com.seu.vcampus.vo.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

/**
 * 图书查询面板
 * 提供图书搜索和浏览功能
 * 
 * @author 虚拟校园开发团队
 * @since 2.2.0
 */
public class BookSearchPanel extends JPanel {
    private VCampusClient client;
    private User currentUser;
    
    private JTable bookTable;
    private DefaultTableModel bookTableModel;
    private JTextField searchField;
    private JButton searchButton;
    private JButton refreshButton;
    private JButton borrowButton;
    
    public BookSearchPanel(VCampusClient client, User user) {
        this.client = client;
        this.currentUser = user;
        
        initializeComponents();
        setupLayout();
        setupEventHandlers();
        loadBooks();
    }
    
    private void initializeComponents() {
        setLayout(new BorderLayout(15, 15));
        setBackground(UIStyleUtil.BACKGROUND_COLOR);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 初始化表格
        String[] columns = {"图书ID", "书名", "作者", "出版社", "ISBN", "库存", "状态"};
        bookTableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        bookTable = new JTable(bookTableModel);
        bookTable.setFont(UIStyleUtil.BODY_FONT);
        bookTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        bookTable.setRowHeight(30);
        
        // 搜索组件
        searchField = UIStyleUtil.createModernTextField("输入书名、作者或ISBN...");
        searchButton = UIStyleUtil.createPrimaryButton("搜索");
        refreshButton = UIStyleUtil.createSecondaryButton("刷新");
        borrowButton = UIStyleUtil.createSuccessButton("借阅");
    }
    
    private void setupLayout() {
        // 搜索面板
        JPanel searchPanel = UIStyleUtil.createCardPanel("图书搜索");
        searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        searchPanel.add(new JLabel("搜索:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        searchPanel.add(refreshButton);
        searchPanel.add(borrowButton);
        
        // 表格面板
        JPanel tablePanel = UIStyleUtil.createCardPanel("图书列表");
        tablePanel.setLayout(new BorderLayout());
        JScrollPane scrollPane = UIStyleUtil.createModernScrollPane(bookTable);
        tablePanel.add(scrollPane, BorderLayout.CENTER);
        
        add(searchPanel, BorderLayout.NORTH);
        add(tablePanel, BorderLayout.CENTER);
    }
    
    private void setupEventHandlers() {
        searchButton.addActionListener(e -> searchBooks());
        refreshButton.addActionListener(e -> loadBooks());
        borrowButton.addActionListener(e -> borrowSelectedBook());
    }
    
    private void loadBooks() {
        // 实现图书加载逻辑
        // 这里可以调用服务端接口获取图书列表
    }
    
    private void searchBooks() {
        // 实现图书搜索逻辑
    }
    
    private void borrowSelectedBook() {
        int selectedRow = bookTable.getSelectedRow();
        if (selectedRow >= 0) {
            String bookId = (String) bookTableModel.getValueAt(selectedRow, 0);
            String bookName = (String) bookTableModel.getValueAt(selectedRow, 1);
            
            // 实现借阅逻辑
            JOptionPane.showMessageDialog(this, 
                "借阅图书: " + bookName + " (ID: " + bookId + ")", 
                "借阅确认", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, 
                "请先选择要借阅的图书", 
                "提示", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void refreshData() {
        loadBooks();
    }
}